/*
 *          		  MidiMaker 
 *       		   (Working title)
 *  		Joe Mirizio & Earl Bellinger
 * 
 *	GNU General Public License version 3.0 (GPLv3)
 */
package midimaker;

import javax.sound.midi.*;

public abstract class InstrumentTrack {
 
    protected final int vel = 64;
    protected final int notedur = 1;
    protected Track tra;
    protected int chan;
    protected int octave;
    protected int[][] notes = {{0, 12, 24, 36, 48, 60, 72, 84, 96, 108, 120}, 	// C
        {2, 14, 26, 38, 50, 62, 74, 86, 98, 110, 122}, 							// D
        {4, 16, 28, 40, 52, 64, 76, 88, 100, 112, 124}, 						// E
        {5, 17, 29, 41, 53, 65, 77, 89, 101, 113, 125}, 						// F
        {7, 19, 31, 43, 55, 67, 79, 91, 103, 115, 127}, 						// G
        {9, 21, 33, 45, 57, 69, 81, 93, 105, 117}, 								// A
        {11, 23, 35, 47, 59, 71, 83, 95, 107, 119}};	  						// B

    public InstrumentTrack(Track t, int c) {
        tra = t;
        chan = c;
        octave = 5; // C Major
    }
    
    public abstract int[][] generateNotes(int[] harmony);

    public Track getTrack() {
        return tra;
    }

    public void setOctave(int o) {
        octave = o;
    }

    private MidiEvent createNoteEvent(int command, int key, int vel, long tick) {
        ShortMessage m = new ShortMessage();
        try {
			m.setMessage(command, chan, key, vel);
		} catch (InvalidMidiDataException e) {
			e.printStackTrace();
			System.exit(1);
		}
        return new MidiEvent(m, tick);
    }

    private void noteOn(int key, long tick) {
        tra.add(createNoteEvent(ShortMessage.NOTE_ON, key, vel, tick));
    }

    private void noteOff(int key, long tick) {
        tra.add(createNoteEvent(ShortMessage.NOTE_OFF, key, 0, tick));
    }

    public void changeProgram(int prog, int tick) {
        ShortMessage sm = new ShortMessage();
        try {
            sm.setMessage(ShortMessage.PROGRAM_CHANGE, chan, prog, 0);
        } catch (InvalidMidiDataException e) {
            e.printStackTrace();
            System.exit(1);
        }
        tra.add(new MidiEvent(sm, tick));
    }

    public void setNotes(int[][] n) {
        for (int[] note : n) {
            int tempOctave = octave;
            if (note.length > 3)
                tempOctave += note[3];
            noteOn(notes[note[0]][tempOctave], (long) note[1]);
            if (note.length > 2)
                noteOff(notes[note[0]][tempOctave], (long) note[1] + note[2]);
            else noteOff(notes[note[0]][tempOctave], (long) note[1] + notedur);
        }
    }
}
// SOME METHODS FROM MATTHIAS PFISTERER WERE USED:
/*
 * Copyright (c) 2000 by Matthias Pfisterer
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 * - Redistributions of source code must retain the above copyright notice,
 *   this list of conditions and the following disclaimer.
 * - Redistributions in binary form must reproduce the above copyright
 *   notice, this list of conditions and the following disclaimer in the
 *   documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
 * OF THE POSSIBILITY OF SUCH DAMAGE.
 */
